package elevador;


/**
 * 
 * @author Victor
 * @author Breno
 */
public class ElevatorControl extends Thread {

	private Elevator elevator;

	public ElevatorControl(Elevator elevator) {
		super("ElevatorControl-" + elevator.getNumber());
		this.elevator = elevator;
	}

	@Override
	public void run() {
		try {
			while (true) {
				while (elevator.isDoorClosed() && elevator.hasRoute()) {
					int dest = elevator.getNextDestination();
					while (elevator.getCurrentFloor() != dest) {
						if (elevator.getCurrentFloor() > dest) {
							elevator.setStatus(Status.DOWNING);
							elevator.updateFloors();
							sleep(1000);
							elevator.setCurrentFloor(elevator.getCurrentFloor() - 1);
						} else {
							elevator.setStatus(Status.UPPING);
							elevator.updateFloors();
							sleep(1000);
							elevator.setCurrentFloor(elevator.getCurrentFloor() + 1);
						}
					}
					elevator.setStatus(Status.STOPPED);
					elevator.setDoorOpened();
					elevator.removeNextDestination();
					elevator.updateFloors();
					elevator.setSensorActived(true);
				}
				sleep(50);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
